 package com.juicygames.undoer.ball;

import android.util.Log;

import com.juicygames.framework.action.UpdateAction;
import com.juicygames.framework.gamepart.GameHandler;
import com.juicygames.framework.gamepart.GamePart;
import com.juicygames.framework.property.IntegerProperty;
import com.juicygames.framework.property.space.CircleProperty;
import com.juicygames.framework.property.space.XVelocity;
import com.juicygames.framework.property.space.YVelocity;

public class UpdateHandler extends GameHandler<UpdateAction> {
	String LOGTAG = UpdateHandler.class.getName();
	
	CircleProperty circle;
	IntegerProperty xVel;
	IntegerProperty yVel;
	OldPosition oldPosition;
	
	public UpdateHandler(GamePart self) {
		super(self);
		
		setActionClassHandled(UpdateAction.class);
		
		self.groundHandler(this);
		
		circle = self.getPart(CircleProperty.class);
		xVel = self.getPart(XVelocity.class);
		yVel = self.getPart(YVelocity.class);
		oldPosition = self.getPart(OldPosition.class);
	}
	
	public void handle(UpdateAction update) {
		
		CircleProperty circle = self.getPart(CircleProperty.class);
		IntegerProperty xVel = self.getPart(XVelocity.class);
		IntegerProperty yVel = self.getPart(YVelocity.class);
		OldPosition oldPosition = self.getPart(OldPosition.class);
		
		oldPosition.get().set(circle.get().getPoint());
		circle.get().offset(xVel.get(),yVel.get());
		
		int x = circle.get().x;
		int y = circle.get().y;
		int r = circle.get().radius;
		if ((x > 320 - r && xVel.get() > 0) || (x < r && xVel.get() < 0)) {
			xVel.set(-xVel.get());
			self.tryToHandleAction(new WallCollideAction());
		}
		if ((y > 480 - r && yVel.get() > 0) || (y < r && yVel.get() < 0)) {
			yVel.set(-yVel.get());
			self.tryToHandleAction(new WallCollideAction());
		}
		
	}

}
